package com.nathan.mq.integration.vender.redismq;

import com.nathan.mq.integration.constant.MQSendTypeEnum;
import com.nathan.mq.integration.constant.MQVenderCS;
import com.nathan.mq.integration.model.AbstractMQ;
import com.nathan.mq.integration.service.IMQSender;
import com.nathan.mq.integration.service.RedisService;
import java.util.concurrent.TimeUnit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

/**
 * rabbitMQ 消息发送器的实现
 *
 * @Description:
 * @Author: zhujunjie
 * @Date: 2021/12/9 17:20
 */
@Component
@ConditionalOnProperty(name = MQVenderCS.YML_VENDER_KEY, havingValue = MQVenderCS.REDIS_MQ)
public class RedisMQSender implements IMQSender {

    @Autowired
    private RedisService redisService;

    @Override
    public void send(AbstractMQ mqModel) {
        if (mqModel.getMQType() == MQSendTypeEnum.QUEUE) {
            redisService.addQueue(mqModel.getMQName(), mqModel.toMessage());
        } else {
            // 订阅模式
            redisService.publish(mqModel.getMQName(), mqModel.toMessage());
        }
    }

    @Override
    public void send(AbstractMQ mqModel, int delay) {
        if (mqModel.getMQType() == MQSendTypeEnum.QUEUE) {
            redisService.addDelayedQueue(mqModel.getMQName(), mqModel.toMessage(), delay, TimeUnit.SECONDS);
        } else {
            // 订阅模式 没有延迟属性
            redisService.publish(mqModel.getMQName(), mqModel.toMessage());
        }
    }

}
